var SpriteLoader = require('RaiSpriteLoader');

cc.Class({
    extends: cc.Component,

    properties: {
        terminalCardUsing: String,//正在使用的终端卡
        isSound: true
    },

    // use this for initialization
    onLoad: function () {
        this.gameMain = cc.find('Canvas').getComponent('HelloWorld');
        this.reviewMain = cc.find('Canvas').getComponent('ReviewMain');
        this.visitMain = cc.find('Canvas').getComponent('visitMain');
        this.terminals = cc.find('Canvas/terminals');
        this.sysPanel = cc.find('Canvas/sysPanel');
        this.lb = cc.find('Canvas/terminals/lineboost');
        this.fw = cc.find('Canvas/terminals/firewall');
        this.vc = cc.find('Canvas/terminals/viruscheck');
        this.nf = cc.find('Canvas/terminals/notfound');
        this.rv = cc.find('Canvas/terminals/randomverification');
        this.zda = cc.find('Canvas/terminals/zerodayattack');
        this.sb = cc.find('Canvas/terminals/sandbox');
        this.terminalCards = [];
        this.terminalCards['lb'] = this.lb;
        this.terminalCards['fw'] = this.fw, 
        this.terminalCards['vc'] = this.vc, 
        this.terminalCards['nf'] = this.nf, 
        this.terminalCards['rv'] = this.rv, 
        this.terminalCards['zda'] = this.zda, 
        this.terminalCards['sb'] = this.sb;
        this.talkInput = cc.find('Canvas/fitLayer/buttonBar/talkBox');
        //设置音乐开关
        this.soundIcon1 = cc.find('Canvas/sysPanel/sound/icon1');
        this.soundIcon2 = cc.find('Canvas/sysPanel/sound/icon2');
        this.soundLabel = cc.find('Canvas/sysPanel/sound/label').getComponent(cc.Label);
        this.isSound = cc.sys.localStorage.getItem('bgmSwitch') != 'true';
        this.onSysSound();
    },

    //初始化终端卡图片
    initTerminalBtn: function(group, terminals)
    {
        this.terminals = terminals;
        for(let key in this.terminalCards)
        {
            this.terminalCards[key].active = false;
        }
        for(let i = 0; i < terminals.length; i++)
        {
            let key = terminals[i];
            let card = this.terminalCards[key];
            card.active = true;
            let spriteName = group + card.name;
            SpriteLoader.load(spriteName, sprite => {
                card.getComponent(cc.Sprite).spriteFrame = sprite;
            });
        }
    },

    onLineBoost: function(){
        this.hide(this.terminals);
        
        if(!this.gameMain.terminalSwitch['lb'])
        {
            if(this.terminalCardUsing !== 'lb'){
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();

                this.gameMain.removeAllBoardEvent();
                this.sendData({'code':'30', 'name':'start LB', data:{}});
                this.terminalCardUsing = 'lb';
                this.terminalCards[this.terminalCardUsing].opacity = 128;
            }else{
                this.gameMain.removeLBEvent();
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();
            }
        }else{
            this.sendData({'code':'30', 'name':'close LB', data:{}});
            //取消其他终端卡的选中效果
            this.unSelectOtherTerminals();
        }
    },

    onFireWall: function(){
        this.hide(this.terminals);

        if(!this.gameMain.terminalSwitch['fw'])
        {
            if(this.terminalCardUsing !== 'fw'){
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();

                this.gameMain.removeAllBoardEvent();
                this.sendData({'code':'40', 'name':'start FW', data:{}});
                this.terminalCardUsing = 'fw';
                this.terminalCards[this.terminalCardUsing].opacity = 128;
            }else{
                this.gameMain.removeFWEvent();
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();
            }
        }else{
            this.sendData({'code':'40', 'name':'close FW', data:{}});
            //取消其他终端卡的选中效果
            this.unSelectOtherTerminals();
        }
        
    },

    onVirusChecker: function(){
        this.hide(this.terminals);

        if(!this.gameMain.terminalSwitch['vc'])
        {
            if(this.terminalCardUsing !== 'vc'){
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();

                this.gameMain.removeAllBoardEvent();
                this.sendData({'code':'50', 'name':'start VC', data:{}});
                this.terminalCardUsing = 'vc';
                this.terminalCards[this.terminalCardUsing].opacity = 128;
            }else{
                this.gameMain.removeVCEvent();
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();
            }
        }
    },

    onNotFound: function(){
        this.hide(this.terminals);

        if(!this.gameMain.terminalSwitch['nf'])
        {
            if(this.terminalCardUsing !== 'nf'){
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();

                this.gameMain.removeAllBoardEvent();
                this.sendData({'code':'60', 'name':'start NF', data:{}});
                this.terminalCardUsing = 'nf';
                this.terminalCards[this.terminalCardUsing].opacity = 128;
            }else{
                this.gameMain.removeNFEvent();
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();
            }
        }
    },

    onRandomVerification: function()
    {
        this.hide(this.terminals);
        if(!this.gameMain.terminalSwitch['rv'])
        {
            if(this.terminalCardUsing !== 'rv'){
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();

                this.gameMain.removeAllBoardEvent();
                this.sendData({'code':'200', 'name':'start RV', data:{}});
                this.terminalCardUsing = 'rv';
                this.terminalCards[this.terminalCardUsing].opacity = 128;
            }else{
                this.gameMain.removeRVEvent();
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();
            }
        }
    },

    onZeroDayAttack: function()
    {
        this.hide(this.terminals);
        if(!this.gameMain.terminalSwitch['zda'])
        {
            if(this.terminalCardUsing !== 'zda'){
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();

                this.gameMain.removeAllBoardEvent();
                this.sendData({'code':'210', 'name':'start ZDA', data:{}});
                this.terminalCardUsing = 'zda';
                this.terminalCards[this.terminalCardUsing].opacity = 128;
            }else{
                this.gameMain.removeZDAEvent();
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();
            }
        }
    },

    onSandbox: function()
    {
        this.hide(this.terminals);
        if(!this.gameMain.terminalSwitch['sb'])
        {
            if(this.terminalCardUsing !== 'sb'){
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();

                this.gameMain.removeAllBoardEvent();
                this.sendData({'code':'230', 'name':'start SB', data:{}});
                this.terminalCardUsing = 'sb';
                this.terminalCards[this.terminalCardUsing].opacity = 128;
            }else{
                this.gameMain.removeSBEvent();
                //取消其他终端卡的选中效果
                this.unSelectOtherTerminals();
            }
        }
    },

    onSysClose: function(){
        this.hide(this.sysPanel);
        this.gameMain.showPop('close');
    },

    onReviewClose: function(){
        this.hide(this.sysPanel);
        this.reviewMain.closePop();
    },

    onVisitClose: function(){
        this.hide(this.sysPanel);
        this.visitMain.closePop();
    },

    onSysSound: function(){
        //切换声音开关状态
        if(this.isSound){
            this.isSound = false;
            cc.sys.localStorage.setItem('bgmSwitch', '');
            this.soundIcon1.active = false;
            this.soundIcon2.active = true;
            this.soundLabel.string = '开启bgm';cc.log('关闭bgm');

            cc.audioEngine.setMusicVolume(0);
        }else{
            this.isSound = true;
            cc.sys.localStorage.setItem('bgmSwitch', 'true');
            this.soundIcon2.active = false;
            this.soundIcon1.active = true;
            this.soundLabel.string = '关闭bgm';

            cc.audioEngine.setMusicVolume(1);cc.log('开启bgm');
        }
    },

    onSendTalk: function(){
        let str = this.talkInput.getComponent(cc.EditBox).string;
        this.talkInput.getComponent(cc.EditBox).string = '';

        this.sendData({'code':'80', 'name':'sendTalk', data:{'str':str}});
    },

    // 显示需要出现的对象
    // @obj: 将出现的对象
    show: function(obj){
        if(obj.active)
            return false;
        obj.active = true;
        var act1=cc.fadeIn(0.15);
        obj.runAction(act1);
        return true;
    },

    // 隐藏对象
    // @obj: 将隐藏的对象
    hide: function(obj){
        if(!obj.active)
            return false;
        var act1=cc.fadeOut(0.15);
        var callback = cc.callFunc(function(){
            obj.active = false;
        },this);
        var seq = cc.sequence(act1, callback);
        obj.runAction(seq);
        return true;
    },

    // 发送消息给服务端
    sendData: function(cmd){
        if(this.gameMain)
            this.gameMain.sendData(cmd);
        else if(this.visitMain)
            this.visitMain.sendData(cmd);
        else
            cc.log('send Data error at btCallFunc');
    },

    //取消其他终端卡的选中效果
    unSelectOtherTerminals: function() {
        if(this.terminalCardUsing == 'rv' && !this.gameMain.terminalSwitch['rv'])
        {
            if(this.terminalCards.hasOwnProperty(this.terminalCardUsing))
                this.terminalCards[this.terminalCardUsing].opacity = 255;
            this.gameMain.removeRVEvent();
        }else if(this.terminalCardUsing == 'fw' && !this.gameMain.terminalSwitch['fw'])
        {
            if(this.terminalCards.hasOwnProperty(this.terminalCardUsing))
                this.terminalCards[this.terminalCardUsing].opacity = 255;
        }else if(this.terminalCardUsing == 'nf' && !this.gameMain.terminalSwitch['nf'])
        {
            if(this.terminalCards.hasOwnProperty(this.terminalCardUsing))
                this.terminalCards[this.terminalCardUsing].opacity = 255;
        }else if(this.terminalCardUsing == 'vc' && !this.gameMain.terminalSwitch['vc'])
        {
            if(this.terminalCards.hasOwnProperty(this.terminalCardUsing))
                this.terminalCards[this.terminalCardUsing].opacity = 255;
        }
        else if(this.terminalCardUsing == 'lb' && !this.gameMain.terminalSwitch['lb'])
        {
            if(this.terminalCards.hasOwnProperty(this.terminalCardUsing))
            this.terminalCards[this.terminalCardUsing].opacity = 255;
        }
        else if(this.terminalCardUsing == 'zda' && !this.gameMain.terminalSwitch['zda'])
        {
            if(this.terminalCards.hasOwnProperty(this.terminalCardUsing))
            this.terminalCards[this.terminalCardUsing].opacity = 255;
        }
        else if(this.terminalCardUsing == 'sb' && !this.gameMain.terminalSwitch['sb'])
        {
            if(this.terminalCards.hasOwnProperty(this.terminalCardUsing))
            this.terminalCards[this.terminalCardUsing].opacity = 255;
        }
        this.terminalCardUsing = '';
    },

    //终端使用完成 不再需要记录正在使用的终端卡
    onTerminalUseFinish: function()
    {
        this.terminalCardUsing = '';
    },

    //被动解除终端卡
    passiveUnuseTerminal: function(terminalName)
    {
        switch(terminalName)
        {
            case 'lb':
            case 'fw':
                    this.terminalCards[terminalName].opacity = 255;
                break;
        }
    }
});
